package com.dibang.app.operation.control.impl;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dibang.app.R;
import com.dibang.app.bean.SearchAdrItem;
import com.dibang.app.mvp.abs.AbsControl;
import com.dibang.app.mvp.util.PickerCity;
import com.dibang.app.mvp.util.Voice;
import com.dibang.app.mvp.util.XmlDB;
import com.dibang.app.operation.control.abs.ByBusLocationControl;
import com.dibang.app.operation.model.abs.ByBusLocationModel;
import com.dibang.app.operation.presenter.abs.ByBusLocationPresenter;
import com.dibang.app.operation.provider.ByBusLocationProImpl;
import com.dibang.app.operation.view.abs.ByBusLocationView;
import com.dibang.app.view.ViewHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import am.widget.stateframelayout.StateFrameLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wxl on 2019/8/5.
 */

public class ByBusLocationControlImpl extends AbsControl<ByBusLocationView, ByBusLocationPresenter, ByBusLocationModel, ByBusLocationProImpl> implements ByBusLocationControl {

    @BindView(R.id.select_location_title_text_view)
    TextView titleTextView;

    @BindView(R.id.select_location_city_text_view)
    TextView cityTextView;

    @BindView(R.id.select_location_input_search_view)
    EditText searchView;

    @BindView(R.id.select_location_home_text_view)
    TextView homeTextView;

    @BindView(R.id.select_location_company_text_view)
    TextView companyTextView;

    @BindView(R.id.select_location_collect_text_view)
    TextView collectTextView;

    @BindView(R.id.select_location_list_state_frame)
    StateFrameLayout stateFrameLayout;

    @BindView(R.id.select_location_list_smart_view)
    SmartRefreshLayout smartView;

    @BindView(R.id.select_location_list_view)
    RecyclerView listView;

    @BindView(R.id.select_location_clear_view)
    TextView clearView;

    private String where;

    public ByBusLocationControlImpl(@NonNull View targetView) {
        super(targetView);
        ButterKnife.bind(this, targetView);
        ViewHelper.setStateSimpleEmptyView(stateFrameLayout, "还没有任何搜索过的痕迹哦~");
        ViewHelper.setStateSimpleLoadingView(stateFrameLayout);
        ViewHelper.setStateSimpleErrorView(stateFrameLayout, "哦豁，在搜索的过程中发生了错误~");
    }


    @OnClick(R.id.select_location_home_layout)
    public void onHomeViewClick(View v) {

    }

    @OnClick(R.id.select_location_company_view)
    public void onCompanyViewClick(View v) {

    }

    @OnClick(R.id.select_location_collect_layout)
    public void onCollectViewClick(View v) {

    }


    @OnClick(R.id.select_location_city_layout)
    public void onCityViewClick(View v) {
        PickerCity.getInstance()
                .setActivity((FragmentActivity) getContext())
                .setListener(city -> {
            cityTextView.setText(city.getName());
            EventBus.getDefault().post(city);
        }).show();
    }


    @OnClick(R.id.select_location_clear_view)
    public void onClearHistoryViewClick(View v) {

    }

    @Override
    public void notifyBusWhereChanged(@NonNull String where) {
        this.where = where;
        setSearchStateAndHint(where);
        getView().notifyHistoryRecordSearch(listView, SearchAdrItem.get());
    }

    /**
     * 设置搜索状态和hint
     * @param where
     */
    private void setSearchStateAndHint(@NonNull String where){
        switch (where) {
            case "start":
                titleTextView.setText("上车地点");
                searchView.setHint("你在哪上车");
                break;
            case "end":
                titleTextView.setText("下车地点");
                searchView.setHint("你在哪下车");
                break;
        }
        String city = XmlDB.get().getString("city");
        if (TextUtils.isEmpty(city)) {
            cityTextView.setText("选择城市");
        } else {
            cityTextView.setText(city);
        }
    }


    @OnClick(R.id.select_location_voice_input_search_view)
    public void startVoiceRecognitionActivity(View v) {
        Voice.startRecordVoice(getContext());
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }
}
